//引入express模块
const express = require('express');
const bodyParser = require('body-parser')
//创建一个app对象，类似一个web 应用（网站）
const app = express();
const fun_sys_dept = require("./modules/fun/fun_sys_dept")
const path = require("path")
const router_sys = require("./router/sys")
const printer_yly = require("./encapsulation/printer/yly")
var uuid = require('node-uuid');
//body数据预处理
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
// 全局 中间件  解决所有路由的 跨域问题
app.all('*',function (req,res,next) {
    res.header('Access-Control-Allow-Origin','*')
    res.header('Access-Control-Allow-Headers','X-Requested-With,Content-Type')
    res.header('Access-Control-Allow-Methods','GET,POST,OPTIONS')
    next()
})
app.use("/static",express.static(path.join(__dirname,'static')))

//接受指定路径的请求，指定回调函数
app.get('/',async function (req, res,next){
    try {
        res.send({
            status:0,data:uuid.v1()
        })
    }catch (e) {
        next(e)
    }
});
app.get('/yly',async function (req, res,next){
    try {
        let yly = new printer_yly("1067219469","2a5f84d93b164ff9ac664a54681d1190","43265922","4004769008","118606793309","8a335ad108af54903486b2664cc3cd09");
        let textPrinter =await yly.fun.textPrinter("<audio>有新的报修工单,9,0</audio><QR>赵宇大傻子</QR><table><tr><td>列1</td><td>列2</td><td><FB>列2</FB></td><td></td></tr></table>","gat1y27gs2h6pnkk6zijrhu6tpcy68fc");
        res.send(textPrinter)
    }catch (e) {
        next(e)
    }
});



app.use('/sys',router_sys)
app.use(function (err, req, res,next) {
    res.send({status:-3,msg:err.message}) // 噢！不!
})
//创建一个web服务器，可以认为就是web服务器对象
//监听8081端口，当监听成功时回调
var server = app.listen(8081, function () {
    var port = server.address().port;  //端口
    console.log("应用实例，访问地址为   http://localhost:%s", port);
})